import os
import sys
import numpy as np
import tensorflow as tf
from tensorflow import keras
import ssl

ssl._create_default_https_context = ssl._create_unverified_context
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
np.set_printoptions(linewidth=1000)

# 加载CIFAR10数据，以numpy形式返回，分割为50000/10000两部分
(x, y), (x_val, y_val) = keras.datasets.cifar10.load_data()
print('training dataset shape: ', x.shape, y.shape)  # (50000, 32, 32, 3) (50000, 1)
print('testing  dataset shape: ', x_val.shape, y_val.shape)  # (10000, 28, 28) (10000,)

print('x[0] in training dataset: \n', x[0], sep='\t', end='\n', file=sys.stdout)

train_dataset = tf.data.Dataset.from_tensor_slices((x, y))
# data shuffle
train_dataset.shuffle(10000)


# data pre-processing
def preprocess(x, y):
    x = tf.cast(x, dtype=tf.float32) / 255.
    y = tf.cast(y, dtype=tf.float32)

res = next(iter(train_dataset))
